﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpCommandBase.h"
#include "GbmpUiPlatformCommandIds.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "CmdStatusUtils.h"
#include "IUserTransaction.h"
#include "IDocument.h"
#include "GbmpModelViewUtil.h"
#include "IGbmp3DView.h"
#include "LevelUtils.h"
#include "IUiDocumentViewManager.h"
#include "IMainWindow.h"
#include "IUiDocument.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

#define BASE_NAME_3D_VIEW       GBMP_TR(L"三维")

//////////////////////////////////////////////////////////////////////////
class CmdCreate3DViewNew : public GbmpCommandBase
{
public:
    CmdCreate3DViewNew();

    virtual bool IsEnabled() const override;
    virtual bool IsVisible() const override;

    virtual OwnerPtr<IAction> ExecuteCommand(const CommandParameters& cmdParams) override;
};

//////////////////////////////////////////////////////////////////////////
REGISTER_GM_COMMAND(CmdCreate3DViewNew);

CmdCreate3DViewNew::CmdCreate3DViewNew()
    : GbmpCommandBase(ID_CMD_GBMP_CREATE_3D_VIEW)
{
}

bool CmdCreate3DViewNew::IsEnabled() const
{
    CMD_ONLY_ENABLE_IN_PROJECT_ENVIRONMENT
}

bool CmdCreate3DViewNew::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

OwnerPtr<IAction> CmdCreate3DViewNew::ExecuteCommand(const CommandParameters& cmdParams)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"创建三维视图"), true);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opTransaction != nullptr, L"事务创建不成功",L"GDMPLab",L"2024-03-30");

    std::wstring viewName;
    bool bOk = GbmpModelViewUtil::GetNextModelViewName(pDoc, View3DViewType, BASE_NAME_3D_VIEW, L" ", viewName);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"GetNextModelViewName失败",L"GDMPLab",L"2024-03-30");

    const auto& levels = LevelUtils::GetLevelsFromLowToHigh(pDoc);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(levels.size() > 0, L"levels为空",L"GDMPLab",L"2024-03-30");
    const ILevel* pLevel = levels[0];
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLevel, L"pLevel为空",L"GDMPLab",L"2024-03-30");

    Gbmp3DViewInput input;
    input.Document = pDoc;
    input.Name = viewName;
    input.Level = pLevel;
    IGbmp3DView* pNew3DView = IGbmp3DView::Create(input);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pNew3DView, L"pNew3DView为空",L"GDMPLab",L"2024-03-30");

    opTransaction->Commit();

    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMPLab",L"2024-03-30");
    IUiDocument* pUiDoc = pUiDocViewMgr->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDoc, L"pUiDoc为空",L"GDMPLab",L"2024-03-30");
    ElementId viewElementId = pNew3DView->GetElementId();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(viewElementId.IsValid(), L"viewElementId无效",L"GDMPLab",L"2024-03-30");

    IMainWindow* pUIMainWnd = IMainWindow::GetMainWindow();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIMainWnd, L"pUIMainWnd无效",L"GDMPLab",L"2024-03-30");

    pUIMainWnd->CreateOrOpenUiView(pUiDoc->GetId(), viewElementId, false, L"");

    return nullptr;
}
